package moltencore.network.protocol;

import spark.network.Util;
import spark.network.protocol.Base;

import java.nio.ByteBuffer;

public class JoinTeam extends Base {
	public static final short TYPE = 0x64;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private byte teamIndex;
	private static final int TEAM_INDEX_LENGTH = Byte.SIZE / 8;

	private byte slotIndex;
	private static final int SLOT_INDEX_LENGTH = Byte.SIZE / 8;

	private static final int SIZE =
			Base.SIZE +
			TEAM_INDEX_LENGTH +
	        SLOT_INDEX_LENGTH;

	public JoinTeam() {
		super(true, TYPE_BYTES);
	}

	public byte getTeamIndex() {
		return teamIndex;
	}

	public void setTeamIndex(int teamIndex) {
		this.teamIndex = (byte)teamIndex;
	}

	public byte getSlotIndex() {
		return slotIndex;
	}

	public void setSlotIndex(int slotIndex) {
		this.slotIndex = (byte)slotIndex;
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		ByteBuffer bb = ByteBuffer.wrap(buffer, Base.SIZE, buffer.length - Base.SIZE);
		bb.put(teamIndex);
		bb.put(slotIndex);
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		ByteBuffer bb = ByteBuffer.wrap(buffer, Base.SIZE, buffer.length - Base.SIZE);
		teamIndex = bb.get();
		slotIndex = bb.get();
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	public static JoinTeam create(byte[] buffer, long receiveTime) {
		JoinTeam packet = new JoinTeam();
		packet.fill(buffer);
		packet.setTimeStampReceive(receiveTime);
		return packet;
	}
}